import { observable, computed, action } from 'mobx';
import ApiHelper from './ApiHelper';
import { message } from 'antd';
import { user_login, checkLogin, logOut } from '../constants/ApiTypes';

class User {
    
    @observable info = null;
    @action async login(username, password, verify) {
        const data = {
            username, password, verify
        };
        const result = await ApiHelper.requestPost(user_login, data, true);
        if (result.status) {
            this.info = result.info;
        } 
        return result;
    }
    @action async checkLogin() {
        const result = await ApiHelper.requestPost(checkLogin);
        if (result.status && result.info.user) {
            this.info = result.info.user;
            return true;
        } else {
            this.info = null;
            return false;
        }
    }
    @action logout() {
        return new Promise(async (resolve, reject) => {
            const result = await ApiHelper.requestPost(logOut);
            if (result.status) {
                this.info = null;
                message.success(result.info, 1.5);
            } else {
                message.error('退出失败', 1.5);
            }
            setTimeout(() => { resolve() }, 1500);
        });
    }
}

export default new User;